package com.youyi.yyviewsdklibrary.Dialog.widget;

import android.content.Context;
import android.support.v4.view.ViewCompat;
import android.support.v4.view.ViewPager;
import android.support.v4.widget.ViewDragHelper;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.widget.FrameLayout;
import com.youyi.yyviewsdklibrary.Dialog.interfaces.OnDragChangeListener;
import com.youyi.yyviewsdklibrary.Dialog.photoview.PhotoView;

public class PhotoViewContainer extends FrameLayout {
    private static final String TAG = "PhotoViewContainer";
    private int HideTopThreshold;
    ViewDragHelper.Callback cb;
    private OnDragChangeListener dragChangeListener;
    private ViewDragHelper dragHelper;
    public boolean isReleasing;
    boolean isVertical;
    private int maxOffset;
    private float touchX;
    private float touchY;
    public ViewPager viewPager;

    public PhotoViewContainer(Context context) {
        this(context, null);
    }

    public PhotoViewContainer(Context context, AttributeSet attributeSet) {
        this(context, attributeSet, 0);
    }

    public PhotoViewContainer(Context context, AttributeSet attributeSet, int i) {
        super(context, attributeSet, i);
        this.HideTopThreshold = 80;
        this.isReleasing = false;
        this.isVertical = false;
        this.cb = new ViewDragHelper.Callback() {
            /* class com.youyi.yyviewsdklibrary.Dialog.widget.PhotoViewContainer.AnonymousClass1 */

            @Override // android.support.v4.widget.ViewDragHelper.Callback
            public int getViewVerticalDragRange(View view) {
                return 1;
            }

            @Override // android.support.v4.widget.ViewDragHelper.Callback
            public boolean tryCaptureView(View view, int i) {
                return !PhotoViewContainer.this.isReleasing;
            }

            @Override // android.support.v4.widget.ViewDragHelper.Callback
            public int clampViewPositionVertical(View view, int i, int i2) {
                int top2 = PhotoViewContainer.this.viewPager.getTop() + (i2 / 2);
                if (top2 >= 0) {
                    return Math.min(top2, PhotoViewContainer.this.maxOffset);
                }
                return -Math.min(-top2, PhotoViewContainer.this.maxOffset);
            }

            @Override // android.support.v4.widget.ViewDragHelper.Callback
            public void onViewPositionChanged(View view, int i, int i2, int i3, int i4) {
                super.onViewPositionChanged(view, i, i2, i3, i4);
                if (view != PhotoViewContainer.this.viewPager) {
                    PhotoViewContainer.this.viewPager.offsetTopAndBottom(i4);
                }
                float abs = (((float) Math.abs(i2)) * 1.0f) / ((float) PhotoViewContainer.this.maxOffset);
                float f = 1.0f - (0.2f * abs);
                PhotoViewContainer.this.viewPager.setScaleX(f);
                PhotoViewContainer.this.viewPager.setScaleY(f);
                view.setScaleX(f);
                view.setScaleY(f);
                if (PhotoViewContainer.this.dragChangeListener != null) {
                    PhotoViewContainer.this.dragChangeListener.onDragChange(i4, f, abs);
                }
            }

            @Override // android.support.v4.widget.ViewDragHelper.Callback
            public void onViewReleased(View view, float f, float f2) {
                super.onViewReleased(view, f, f2);
                if (Math.abs(view.getTop()) <= PhotoViewContainer.this.HideTopThreshold) {
                    PhotoViewContainer.this.dragHelper.smoothSlideViewTo(PhotoViewContainer.this.viewPager, 0, 0);
                    PhotoViewContainer.this.dragHelper.smoothSlideViewTo(view, 0, 0);
                    ViewCompat.postInvalidateOnAnimation(PhotoViewContainer.this);
                } else if (PhotoViewContainer.this.dragChangeListener != null) {
                    PhotoViewContainer.this.dragChangeListener.onRelease();
                }
            }
        };
        init();
    }

    private void init() {
        this.HideTopThreshold = dip2px((float) this.HideTopThreshold);
        this.dragHelper = ViewDragHelper.create(this, this.cb);
        setBackgroundColor(0);
    }

    /* access modifiers changed from: protected */
    public void onFinishInflate() {
        super.onFinishInflate();
        this.viewPager = (ViewPager) getChildAt(0);
    }

    /* access modifiers changed from: protected */
    public void onSizeChanged(int i, int i2, int i3, int i4) {
        super.onSizeChanged(i, i2, i3, i4);
        this.maxOffset = getHeight() / 3;
    }

    public boolean dispatchTouchEvent(MotionEvent motionEvent) {
        boolean z = true;
        if (motionEvent.getPointerCount() > 1) {
            return super.dispatchTouchEvent(motionEvent);
        }
        try {
            int action = motionEvent.getAction();
            if (action != 0) {
                if (action != 1) {
                    if (action == 2) {
                        float x = motionEvent.getX() - this.touchX;
                        float y = motionEvent.getY() - this.touchY;
                        this.viewPager.dispatchTouchEvent(motionEvent);
                        if (Math.abs(y) <= Math.abs(x)) {
                            z = false;
                        }
                        this.isVertical = z;
                        this.touchX = motionEvent.getX();
                        this.touchY = motionEvent.getY();
                    } else if (action != 3) {
                    }
                }
                this.touchX = 0.0f;
                this.touchY = 0.0f;
                this.isVertical = false;
            } else {
                this.touchX = motionEvent.getX();
                this.touchY = motionEvent.getY();
            }
        } catch (Exception unused) {
        }
        return super.dispatchTouchEvent(motionEvent);
    }

    private boolean isTopOrBottomEnd() {
        View currentImageView = getCurrentImageView();
        if (!(currentImageView instanceof PhotoView)) {
            return false;
        }
        PhotoView photoView = (PhotoView) currentImageView;
        if (photoView.attacher.isTopEnd || photoView.attacher.isBottomEnd) {
            return true;
        }
        return false;
    }

    private View getCurrentImageView() {
        ViewPager viewPager2 = this.viewPager;
        FrameLayout frameLayout = (FrameLayout) viewPager2.getChildAt(viewPager2.getCurrentItem());
        if (frameLayout == null) {
            return null;
        }
        return frameLayout.getChildAt(0);
    }

    public boolean onInterceptTouchEvent(MotionEvent motionEvent) {
        boolean shouldInterceptTouchEvent = this.dragHelper.shouldInterceptTouchEvent(motionEvent);
        if (motionEvent.getPointerCount() > 1 && motionEvent.getAction() == 2) {
            return false;
        }
        if (isTopOrBottomEnd() && this.isVertical) {
            return true;
        }
        if (!shouldInterceptTouchEvent || !this.isVertical) {
            return false;
        }
        return true;
    }

    public boolean onTouchEvent(MotionEvent motionEvent) {
        if (motionEvent.getPointerCount() > 1) {
            return false;
        }
        try {
            this.dragHelper.processTouchEvent(motionEvent);
        } catch (Exception unused) {
        }
        return true;
    }

    public void computeScroll() {
        super.computeScroll();
        if (this.dragHelper.continueSettling(false)) {
            ViewCompat.postInvalidateOnAnimation(this);
        }
    }

    public int dip2px(float f) {
        return (int) ((f * getContext().getResources().getDisplayMetrics().density) + 0.5f);
    }

    public void setOnDragChangeListener(OnDragChangeListener onDragChangeListener) {
        this.dragChangeListener = onDragChangeListener;
    }

    /* access modifiers changed from: protected */
    public void onDetachedFromWindow() {
        super.onDetachedFromWindow();
        this.isReleasing = false;
    }
}
